#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>

using namespace std;

class Solution {
public:
    string licenseKeyFormatting(string s, int k) {
      string res;
      // 入参校验

      // 去除-，并且将字符转换为大写
      res = GetCharAndToUpper(s);

      // 利用k进行格式化
      res = FormatUseK(res, k);
      return res;
   }

   string GetCharAndToUpper(string s)
   {
      /* 内部函数，入参合法性由调用者保证 */
      string res;
      for (auto ch : s) {
         if (ch != '-') {
            res.push_back(toupper(ch));
         }
      }
      return res;
   }

   string FormatUseK(string s, int k)
   {
      string res;
      reverse(s.begin(), s.end());
      int len = s.length();
      if (len == 0) {
         return res;
      }

      int kTmp = k;
      for (int idx = 0; idx < len; idx++) {
         res.push_back(s[idx]);
         kTmp--;
         if (kTmp == 0) {
            res.push_back('-');
            kTmp = k;
         }
      }
      if (res.back() == '-') {
         res.pop_back();
      }
      reverse(res.begin(), res.end());
      return res;
   }
};

int main()
{
   Solution s;
   string str{"5-fdd"};
   string res = s.licenseKeyFormatting(str, 2);
   cout << res << endl;

   return 0;
}

